package com.fh.pms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fh.pms.entity.*;
import com.fh.pms.mapper.ProductMapper;
import com.fh.pms.service.*;
import com.fh.result.ResultObject;
import com.fh.search.ProductBusiness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author F
 * @since 2021-05-24
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {

   @Autowired
    private IMemberPriceService memberPriceService;
    @Autowired
    private IProductLadderService productLadderService;
    @Autowired
    private IPmsBrandService brandService;
    @Autowired
    private IProductCategoryService productCategoryService;

    @Override
    @Transactional
    public void saveOrUpdateProduct(ProductBusiness productBusiness) {
        Product product=productBusiness;
        if(product.getId() == null){
            product.setCreateTime(new Date());
        }
        product.setUpdateTime(new Date());

        //根据id查询商品名称
        PmsBrand byId = brandService.getById(productBusiness.getBrandId());
        product.setBrandName(byId.getName());
        //查询商品的分类名称
        ProductCategory byId1 = productCategoryService.getById(productBusiness.getProductCategoryId());
        product.setProductCategoryName(byId1.getName());

        //保存商品的主表数据
        saveOrUpdate(product);
        //保存会员价格
        saveOrUpdateProduct(productBusiness,product.getId());
        //保存阶梯价格
        saveProductLadder(productBusiness,product.getId());
    }

    @Override
    public ResultObject updateDeleteStatus(Product product) {
        product.setDeleteStatus(1);
        super.updateById(product);
        return ResultObject.success();
    }


    private void saveOrUpdateProduct(ProductBusiness productBusiness, Long productId) {
    //先删除商品的阶梯价格
        QueryWrapper<MemberPrice>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_Id", productId);
        memberPriceService.remove(queryWrapper);
        List<MemberPrice>list=productBusiness.getProductMemberPriceList();
        list.forEach(memberPrice -> {
            memberPrice.setProductId(productId);
        });
        memberPriceService.saveBatch(list);

    }
    private void saveProductLadder(ProductBusiness productBusiness, Long productId) {
        QueryWrapper<ProductLadder>queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("product_Id", productId);
        productLadderService.remove(queryWrapper);

        List<ProductLadder>list=productBusiness.getProductLadderList();
        list.forEach(productLadder -> {
            productLadder.setProductId(productId);
        });
        productLadderService.saveBatch(list);

    }

}
